package com.common.android.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.product.android.business.ApplicationVariable;

/**
 * 上机网络相关工具类
 * <br>Created 2014-8-22 下午4:45:59
 * @version  
 * @author   huangyx		
 * @see 	 
 * Copyright(c) 2009-2014, TQ Digital Entertainment, All Rights Reserved
 *
 */
public class NetWorkUtils {

    private static final int CMCC = 1;   //中国移动
    private static final int CUCC = 2;   //中国联通
	private static final int CTCC = 3;   //中国电信 
    
	/**
     * 获取手机接入的运营商网络类型
     * <br>Created 2014-8-22 下午4:37:52
     * @param context 上下文
     * @return 手机接入的运营商网络类型
     * @author       huangyx
     */
	public static int getAPNType(Context context) {
		int netType = -1;
		ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null) {
			return netType;
		}
		int nType = networkInfo.getType();
		if (nType == ConnectivityManager.TYPE_MOBILE) {
			String info=networkInfo.getExtraInfo().toLowerCase();
			if (info.equals("cmnet")||info.equals("cmwap")) {
				netType = CMCC;
			} else  if(info.equals("3gwap")||info.equals("3gnet")||info.equals("uniwap")||info.equals("uninet")){
				netType = CUCC;
			}else if (info.equals("ctwap")||info.equals("ctnet")) {
				netType = CTCC;
			}
		}
		return netType;
	}
	
    /**
     * 判断网络状态
     * <br>Created 2014-8-22 下午1:47:27
     * @param context 上下文
     * @return 网络连接正常返回true，否则返回false
     * @author       huangyx       
     */
    public static boolean JudgeNetWorkStatus(Context context) {
        if (context == null){
            return false;
        }
        boolean netStatus = false;
        ConnectivityManager conManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info=conManager.getActiveNetworkInfo();
        if (info!= null) {
            netStatus = info.isAvailable();
        }
        // Log.w(TAG, "网络是否连接异常netStatus :" + netStatus);
        return netStatus;
    }
    
	/**
	 * 判断是否WIFI环境
	 * <br>Created 2014-10-31 上午10:29:05
	 * @return true表示当前是WIFI
	 * @author  cb
	*/
	public static boolean isWifi(){
		Context context=ApplicationVariable.INSTANCE.applicationContext;
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);//获取系统的连接服务
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();//获取网络的连接情况	
		if(activeNetInfo == null) {
			return false;
		}
		if(activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI){
			// wifi
			return true;
		}
		return false;
	}
}
